﻿using T4UtilityBelt.UxDesigns.Controls.Renderers.Android.Helpers;
using T4UtilityBelt.Writers;

namespace T4UtilityBelt.UxDesigns.Controls.Renderers.Android
{
    public abstract class ControlRenderer<TControl, TRenderer> : IControlRenderer<TControl, TRenderer> 
        where TControl : Control<TControl, TRenderer>
        where TRenderer : class, IControlRenderer<TControl, TRenderer>
    {
        #region Methods

        public void RenderUiXmlBegin(TControl control, CodeWriter writer)
        {
            if (control != null)
            {
                OnRenderUiXmlBegin(control, writer);
            }
        }

        public void RenderUiXml(TControl control, CodeWriter writer)
        {
            if (control != null)
            {
                OnRenderUiXmlBegin(control, writer);
                OnRenderUiXmlContent(control, writer);
                OnRenderUiXmlEnd(control, writer);
            }
        }

        public void RenderUiXmlEnd(TControl control, CodeWriter writer)
        {
            if (control != null)
            {
                OnRenderUiXmlEnd(control, writer);
            }
        }

        protected virtual void OnRenderUiXmlContent(TControl control, CodeWriter writer)
        {
        }

        protected virtual void OnRenderUiXmlBegin(TControl control, CodeWriter writer)
        {
        }

        protected virtual void OnRenderUiXmlEnd(TControl control, CodeWriter writer)
        {
        }

        public void RenderUiCode(TControl control, CodeWriter writer)
        {
            OnRenderUiCode(control, writer);
        }

        protected virtual void OnRenderUiCode(TControl control, CodeWriter writer)
        {

        }

        protected virtual string GetRenderedUid(TControl control)
        {
            return control.Uid;
        }

        protected void RenderControlAttributes(TControl control, CodeWriter writer)
        {
            HeightWriter.Write(control.Height, control.HorizontalAlignment, writer);
            WidthWriter.Write(control.Width, control.VerticalAlignment, writer);
        }

        protected void RenderFirstElementAttributes(CodeWriter writer)
        {
            if (writer.IsFirstElement)
            {
                writer.IsFirstElement = false;
                writer.WriteAttribute("xmlns:android", "http://schemas.android.com/apk/res/android");
                writer.WriteAttribute("xmlns:local", "http://schemas.android.com/apk/res/com.conference");
            }
        }

        #endregion Methods
    }
}